﻿using CRM.Database.Model;
using CRM.DbInterface;

namespace CRM.Syncroniser
{
 public class TrainingCentersSyncroniser:ISyncronise
 {


     private DbInterface.LearningProviders cdbTC;

     /// <summary>
     /// Центры обучения из CDB
     /// </summary>
     public LearningProviders CDBTC
     {
         get
         {
             if (cdbTC == null)
                 cdbTC = DataLayer.LearningProviders;
             return cdbTC;
         }
     }

     private CRM.Database.Model.TrainingsCentersCollection crmTC;
     
     /// <summary>
     /// Центры обучения из CRM
     /// </summary>
     public  CRM.Database.Model.TrainingsCentersCollection CRMTC
     {
         get
         {
             if(crmTC==null)
                 crmTC = new TrainingsCentersCollection();
             return crmTC;
         }
     }


     #region Implementation of ISyncronise

     /// <summary>
     /// Запуск синхронизации
     /// </summary>
     /// <returns>Результат запуска</returns>
     public bool Syncronise()
     {
         foreach(var cdbItem in CDBTC)
         {
             var crmItem = new TrainingsCenters
                               {
                                   CommentsRu = cdbItem.Comment,
                                   Email = cdbItem.Email,
                                   CommentsEn = "",
                                   IsActive = true,
                                   NameRu = cdbItem.Name,
                                   Phone = cdbItem.Phone
                               };
           if(CRMTC.GetItemByName(crmItem.NameRu)==null)  
               CRMTC.Add(crmItem, "Max");
         }

         return true;
     }

     #endregion
    }
}
